Editor.Shapes.VocabClass = function (u, i) {
	if (i == undefined) {
		i = this;
	}
	Editor.Shapes.ResourceConnectable.call(this, u, i);

	this.line = new Editor.Decks.LineVocabClass();
	this.line.init(this);

	this.vocProps = new Array();
}

Editor.Util.inherits(Editor.Shapes.VocabClass, Editor.Shapes.ResourceConnectable);

Editor.Shapes.VocabClass.prototype.alignTextVertical = 'top';

Editor.Shapes.VocabClass.prototype.draw = function () {
	Editor.Shapes.VocabClass.superClass_.draw.call(this, this);
	this.line.draw();
	//this._resize(Editor.CONFIG.VOCABCLASS_SHAPE_WIDTH - this.getWidth(), Editor.CONFIG.VOCABCLASS_SHAPE_HEIGHT - this.getHeight());
}

Editor.Shapes.VocabClass.prototype.getFigure = function () {
	return Editor.STATE.CANVAS.rect(this.getX(), this.getY(), this.getWidth(), this.getHeight(), Editor.CONFIG.VOCABCLASS_SHAPE_RADIUS).attr(this.getShapeAttr());
}

Editor.Shapes.VocabClass.prototype.remove = function () {
	var arrProperties = Array();
	for (var i = 0; i < this.inProperties.length; i++) {
		var s = Editor.STATE.SHAPES.get(this.inProperties[i]);
		if (!s.getIsDeleted()) {
			arrProperties.push(this.inProperties[i]);
		}
	}

	var arrVocProperties = Array();
	for (var i = 0; i < this.vocProps.length; i++) {
		var s = Editor.STATE.SHAPES.get(this.vocProps[i]);
		if (!s.getIsDeleted()) {
			arrVocProperties.push(this.vocProps[i]);
		}
	}

	var resource = this;
	var impl = this;
	var line = this.line;

	var arrConnections = new Array();

	var cmd = new JS.Command({
		execute: function () {
			Editor.Shapes.VocabClass.superClass_._remove.call(impl);
			line.remove();
			for (var i = 0; i < arrProperties.length; i++) {
				var s = Editor.STATE.SHAPES.get(arrProperties[i]);
				if (!s.getIsDeleted()) {
					s._remove();
					Editor.Util.removeConnections(s.getUuid(),arrConnections);
				}
			}
			for (var i = 0; i < arrVocProperties.length; i++) {
				var s = Editor.STATE.SHAPES.get(arrVocProperties[i]);
				if (!s.getIsDeleted()) {
					s._remove();
					Editor.Util.removeConnections(s.getUuid(),arrConnections);
				}
			}
			Editor.Util.removeConnections(impl.getUuid(),arrConnections);
		},
		undo: function () {
			impl.draw();
			for (var i = 0; i < arrProperties.length; i++) {
				var s = Editor.STATE.SHAPES.get(arrProperties[i]);
				if (s.getIsDeleted()) {
					s.draw();
				}
			}
			for (var i = 0; i < arrVocProperties.length; i++) {
				var s = Editor.STATE.SHAPES.get(arrVocProperties[i]);
				if (s.getIsDeleted()) {
					s.draw();
				}
			}
			Editor.Util.unremoveConnections(arrConnections);
			impl.updateVocabProperties();
		},
		stack: Editor.STATE.CMDSTACK
	});
	cmd.name = 'remove vocabclass';
	cmd.execute();
}


Editor.Shapes.VocabClass.prototype.addVocabPropertie = function (uuid) {
	var v = Editor.STATE.SHAPES.get(uuid);
	if (!v.getDomain()) {
		this.vocProps.push(uuid);
		v.setDomain(this.getUuid());
	} else if (v.getDomain() != this.getUuid()) {
		var c = Editor.STATE.SHAPES.get(v.getDomain());
		c.removeVocabPropertie(uuid);
		this.vocProps.push(uuid);
		v.setDomain(this.getUuid());
		c.updateVocabProperties();
	} else {
		this.vocProps.sort(function (a, b) {
			return Editor.STATE.SHAPES.get(a).getY() - Editor.STATE.SHAPES.get(b).getY()
		});
	}
}

Editor.Shapes.VocabClass.prototype.removeVocabPropertie = function (uuid) {
	var deleted = false;
	for (var i = 0; i < this.vocProps.length; i++) {
		if (!deleted && this.vocProps[i] == uuid) {
			this.vocProps.splice(i, 1);
			deleted = true;
		}
	}
}

Editor.Shapes.VocabClass.prototype.updateVocabProperties = function () {
	var delta = Editor.CONFIG.DECK_VOCABCLASS_DELTA_PROPS;
	var x = this.getX() + delta;
	var y = this.getY() + Editor.CONFIG.DECK_LINECLASS_DELTA_Y + delta;

	var w = this.getMinWidth();
	this.minWidth = w;

	for (var i = 0; i < this.vocProps.length; i++) {
		var vp = Editor.STATE.SHAPES.get(this.vocProps[i]);
		if (!vp.getIsDeleted()) {
			vp.moveTo(x, y);
			y += vp.getHeight() + delta;
			if ((vp.getWidth() + delta * 2) > w) {
				w = vp.getWidth() + delta * 2;
			}
			if ((vp.getMinWidth() + delta * 2) > this.minWidth) {
				this.minWidth = vp.getMinWidth() + delta * 2;
			}
		}
	}
	if (this.vocProps.length == 0) {
		y += delta;
		this.minWidth = this.getMinWidth()
	}

	for (var i = 0; i < this.vocProps.length; i++) {
		var vp = Editor.STATE.SHAPES.get(this.vocProps[i]);
		if (!vp.getIsDeleted()) {
			if (vp.getWidth() < (w - delta * 2)) {
				vp._resize((w - delta * 2 - vp.getWidth()), 0);
			}
		}
	}
	y += delta;
	if (this.vocProps.length == 0) {
		this._resize((this.getMinWidth() - w), ((y - this.getY()) - this.getHeight()));
	} else {
		this._resize((w - this.getWidth()), ((y - this.getY()) - this.getHeight()));
	}
	this.toFrontVocabProperties();
}

Editor.Shapes.VocabClass.prototype.toFrontVocabProperties = function () {
	for (var i = 0; i < this.vocProps.length; i++) {
		var vp = Editor.STATE.SHAPES.get(this.vocProps[i]);
		if (!vp.getIsDeleted()) {
			vp.toFront();
		}
	}
}

Editor.Shapes.VocabClass.prototype.toFront = function () {
	Editor.Shapes.VocabClass.superClass_.toFront.call(this);
	this.line.toFront();
	this.toFrontVocabProperties();
}

Editor.Shapes.VocabClass.prototype._remove = function () {
	Editor.Shapes.VocabClass.superClass_._remove.call(this);
	this.line.remove();
}

Editor.Shapes.VocabClass.prototype.translate = function (dx, dy) {
	if (dx != 0 || dy != 0) {
		Editor.Shapes.VocabClass.superClass_.translate.call(this, dx, dy);
		this.line.translate(dx, dy);
		this.updateVocabProperties();
	}
}

Editor.Shapes.VocabClass.prototype._translate = function (dx, dy) {
	if (dx != 0 || dy != 0) {
		Editor.Shapes.VocabClass.superClass_._translate.call(this, dx, dy);
		this.line.translate(dx, dy);
		this.updateVocabProperties();
	}
}

Editor.Shapes.VocabClass.prototype._resize = function (dw, dh) {
	if (dw != 0 || dh != 0) {
		Editor.Shapes.VocabClass.superClass_._resize.call(this, dw, dh);
		this.line.redraw();
		this.line.toFront();
	}
}

Editor.Shapes.VocabClass.prototype.resize = function (dw, dh) {
	if (dw != 0) {
		for (var i = 0; i < this.vocProps.length; i++) {
			var vp = Editor.STATE.SHAPES.get(this.vocProps[i]);
			if (!vp.getIsDeleted()) {
				vp._resize(dw, 0);
			}
		}
	}
	if (dw != 0 || dh != 0) {
		Editor.Shapes.VocabClass.superClass_.resize.call(this, dw, dh);
		this.line.redraw();
		this.line.toFront();
		this.updateVocabProperties();
	}
}

Editor.Shapes.VocabClass.prototype.getTypeClass = function () {
	return "vocabClass";
}

Editor.Shapes.VocabClass.prototype.getInfo = function () {
	var str = '';
	if (Editor.CONFIG.DEBUG) {
		str = 'Vocab Class:\n';
		str += '---------------\n';
		for (var i = 0; i < this.vocProps.length; i++) {
			var vp = Editor.STATE.SHAPES.get(this.vocProps[i]);
			str += ' ' + vp.getName() + '\n';
		}
		str += '---------------\n';
	}
	str += Editor.Shapes.VocabClass.superClass_.getInfo.call(this);
	return str;
}

Editor.Shapes.VocabClass.prototype.toObject = function () {
	var strVocProps = '';
	for (var i = 0; i < this.vocProps.length; i++) {
		strVocProps +=  this.vocProps[i] + ' ';
	}
	return {
		'type': 'vocabClass',
		'vocProps': strVocProps,
		'resource': Editor.Shapes.VocabClass.superClass_.toObjectResource.call(this)
	}
}

Editor.Shapes.VocabClass.prototype.getShapeAttr = function () {
	return Editor.CONFIG.VOCABCLASS_SHAPE_ATTR;
}

Editor.Shapes.VocabClass.prototype.getMinWidth = function () {
	if (this.minWidth) {
		return this.minWidth;
	} else {
		return Editor.Shapes.VocabClass.superClass_.getMinWidth.call(this);
	}
}

Editor.Shapes.VocabClass.prototype.getVocProps = function () {
	return this.vocProps;
}
